package com.acme.task1;

import java.util.Arrays;
/**
 * Calculator
 * Can solve strings like 56 + 6 '*' 5 + 6 '*' 10 / 5 with priority sensitive
 * TODO: Add supporting of (brackets)
 * TODO: Add supporting of strings witch starts with '-'
 * @author Inal Kardanov
 *
 */

public class Calculator {
	
	
	/**
	 * Remove one element from array by index
	 * @param original Original array
	 * @param element Index of element, witch we have to delete
	 * @return String[]
	 */
	public static String[] removeElement(String[] original, int element){
		String[] n = new String[original.length - 1];
	    System.arraycopy(original, 0, n, 0, element );
	    System.arraycopy(original, element+1, n, element, original.length - element-1);
	    return n;
	}
	
	/**
	 * Solve one priority operation (* or /) and returns new array 
	 * @param args
	 * @return String[] 
	 */
	private static String[] solvePriorities(String[] args){
		final String[] PRIORITY_OPERATORS = {"/", "'*'"}; //Allowed operations in this function
		char currentOperator; // Char with current operation
		int operatorIndex = -1;
		double operand1, operand2, operationResult;
		do{ // search for first ('*' OR '/') operator
			operatorIndex++;
		}while (Arrays.asList(PRIORITY_OPERATORS).contains(args[operatorIndex]) == false && operatorIndex < args.length -1); // loop while we'll find first operator or array ends
		if (operatorIndex != 0 && operatorIndex != args.length -1){ // If we found operator
			currentOperator = args[operatorIndex].charAt(0);
			operand1 = Double.parseDouble(args[operatorIndex-1]); // Get left number
			operand2 = Double.parseDouble(args[operatorIndex+1]); // Get right number
			if (currentOperator == '/')
				operationResult = operand1 / operand2;
			else
				operationResult = operand1 * operand2;
			args = removeElement(args, operatorIndex+1); // Remove second number
			args = removeElement(args, operatorIndex); // Remove operator
			args[operatorIndex-1] = Double.toString(operationResult); //Replace first number by result of operation
			args = solvePriorities(args); // Make it recursive, while will solve all priority operations
		}
		return args;
	}
	/**
	 * Solve simple operations 
	 * @return
	 */
	private static double solveSimple(String[] args){
		double result = 0, currentDigit = 0;
		for (int i = 0; i < args.length; i += 2){
			currentDigit = Double.parseDouble(args[i]);
			if (i != 0)
				if (args[i-1].charAt(0) == '-')
					currentDigit *= -1;
			result += currentDigit;
		}
		return result;
	}

	//operands[j++] = 
	
	public static void main(String[] args) {
		final String[] OPERATORS = {"+", "-", "/", "'*'"}; // allowed operations
		String[] operands; // Array of operands
		double result = 0;
		int i = 0, j = 0;
		boolean stringError = false; // error in string parsing (flag)
		if (args.length >= 3){
			operands = new String[args.length];
			for (i = 0; i < args.length; i++){ //Let's check string
				if (!stringError){
					if (i % 2 == 0){ // even, should be digit
						try{  
						    Double.parseDouble(args[i]);  
						}
						catch(NumberFormatException nfe){
							stringError = true;
							System.out.println("Error in digit parsing: "+args[i]);
						}
					}else{ //odd, should be an operator
						if (Arrays.asList(OPERATORS).contains(args[i]) == false){ // check if operator is allowed
								stringError = true;
								System.out.println("Should be operator "+args[i]);
							}
					}
				}
			}
			if (stringError == false){ // Everything is OK
				System.out.println(Arrays.toString(args));
				operands = solvePriorities(args);
				System.out.println(Arrays.toString(operands));
				result = solveSimple(operands);
				System.out.println("Result: "+result);
			}else{ // Something go wrong
				System.out.println("Error in string parsing");
			}
		}
	}
}
